package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Name SetmealController
 * @Author stella
 * @Date 2023-03-05 7:34 PM
 */
@RestController
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 套餐列表
     * @return
     */
    @GetMapping("/setmeal/page")
    public ResultInfo findPage(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer pageSize,String name){
        Page<Setmeal> setmealPage = setmealService.findPage(page,pageSize,name);
        return ResultInfo.success(setmealPage);
    }

    /**
     * 回显套餐内容
     * @param id
     * @return
     */
    @GetMapping("/setmeal/{id}")
    public ResultInfo findOne(@PathVariable(value = "id") Long id){
        Setmeal setmeal = setmealService.findOne(id);
        return ResultInfo.success(setmeal);
    }

    /**
     * 新增套餐
     * @param setmeal
     * @return
     */
    @CacheEvict(value = "setmeal", key = "#setmeal.categoryId")
    @PostMapping("/setmeal")
    public ResultInfo save(@RequestBody Setmeal setmeal){
        setmealService.save(setmeal);
        return ResultInfo.success();
    }

    /**
     * 批量启停售
     * @param status
     * @param ids
     * @return
     */
    @CacheEvict(value = "setmeal",allEntries = true)
    @PostMapping("/setmeal/status/{status}")
    public ResultInfo updateStatus(@PathVariable ("status")Integer status,
                                   @RequestParam List<Long> ids){
        setmealService.updateStatus(status,ids);
        return ResultInfo.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @CacheEvict(value = "setmeal",allEntries = true)
    @DeleteMapping("/setmeal")
    public ResultInfo delete(@RequestParam  List<Long>ids){
        setmealService.delete(ids);
        return ResultInfo.success();
    }
}
